//
//  ProducersPicker.m
//  YaoHeAPP
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "ProducersPicker.h"
#import "Controller.h"
@implementation ProducersPicker

#define App_Frame_Height        [UIScreen mainScreen].bounds.size.height
#define App_Frame_Width         [[UIScreen mainScreen] bounds].size.width
#define Line_Low_Color   [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1]
//红色
#define  RedColor  [UIColor colorWithRed:216.0/255.0 green:12.0/255.0 blue:24.0/255.0 alpha:1]



+(void)showPickerView:(UIView*)selfView withTitle:(NSString*)titleString  selectString:(NSString*)str andSelectBlock:(void(^)(NSString*))selectRow
{
    //创建pickerView
    ProducersPicker *pickerView = [[ProducersPicker alloc] initWithFrame:selfView.bounds];
    pickerView.SelectBlock = selectRow;
    pickerView.titleStr = titleString;
    pickerView.selectAry = str;
    [pickerView setSubviews];
    [selfView addSubview:pickerView];
}
//初始化大小
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfAction)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self performSelector:@selector(showSelfAction) withObject:nil afterDelay:0];
    return self;
}

//设置，要弹出的视图上显示的东西
-(void)setSubviews {
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, App_Frame_Width, 260)];
    _bgView.backgroundColor = Line_Low_Color;
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = .3;
    _bgView.layer.shadowOffset = CGSizeMake(0, -1);
    [self addSubview:_bgView];
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake((App_Frame_Width - 175)/2, 0, 140, 40)];
    titleL.backgroundColor = [UIColor clearColor];
    titleL.font = [UIFont systemFontOfSize:16];
    titleL.textColor = [UIColor whiteColor];
    titleL.text = self.titleStr;
    [_bgView addSubview:titleL];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.backgroundColor = [UIColor clearColor];
    confirmBtn.layer.cornerRadius = 4;
    confirmBtn.layer.borderWidth = 0.5;
    confirmBtn.layer.borderColor = RedColor.CGColor;
    confirmBtn.frame = CGRectMake(App_Frame_Width - 75, 7, 60, 32);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:RedColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:confirmBtn];
    
    UIButton *bBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bBtn.backgroundColor = [UIColor clearColor];
    bBtn.layer.cornerRadius = 4;
    bBtn.layer.borderWidth = 0.5;
    bBtn.layer.borderColor = RedColor.CGColor;
    bBtn.frame = CGRectMake(App_Frame_Width - 140, 7, 60, 32);
    [bBtn setTitle:@"不限" forState:UIControlStateNormal];
    [bBtn setTitleColor:RedColor forState:UIControlStateNormal];
    [bBtn addTarget:self action:@selector(bBtn) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:bBtn];
     _tagArray=[[NSMutableArray alloc]init];
    //获取生产商
    [Controller requestWithURL:@"AppPurchaseInfo/PostProducersInfo?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
        _dataArray=[[NSMutableArray alloc]init];
        NSMutableArray*array=classData.data;
        _dataArray=array;
        //创建一个滑动视图
        _scrollView=[[UIScrollView alloc]init];
        _scrollView.frame=CGRectMake(0, 44, App_Frame_Width, 216);
        _scrollView.contentSize = CGSizeMake(0,_dataArray.count/2*(App_Frame_Width/2*0.2+18)+(_dataArray.count%2)*50);
        _scrollView.backgroundColor=[UIColor whiteColor];
        [_bgView addSubview:_scrollView];
        
        NSArray *selectedArray = [_selectAry componentsSeparatedByString:@","];
        
        for (NSInteger i = 0; i<array.count; i++) {
            //创建按钮
            UIButton *manufacturerButton = [UIButton buttonWithType:UIButtonTypeCustom];
            manufacturerButton.tag=100+i;
            manufacturerButton.frame=CGRectMake(App_Frame_Width/20*1+App_Frame_Width/2*(i%2), 15+((App_Frame_Width/2*0.2+15)*(i/2)), App_Frame_Width/2*0.8, App_Frame_Width/2*0.2);
            [manufacturerButton setTitle:_dataArray[i][@"CONDITION_NAME"] forState:UIControlStateNormal];
            
            for (NSString *tempString in selectedArray) {
                if ([tempString isEqualToString:_dataArray[i][@"CONDITION_NAME"]]) {
                    manufacturerButton.layer.borderColor = [UIColor orangeColor].CGColor;
                    NSNumber *number = [NSNumber numberWithInteger:100+i];
                    [_tagArray addObject:number];
                    manufacturerButton.selected = YES;
                    break;
                }
            }
            
            [manufacturerButton setBackgroundImage:[UIImage imageNamed:@"bg_multi_select_item_selected.9"] forState:UIControlStateSelected];
            [manufacturerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [manufacturerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
            manufacturerButton.layer.borderWidth = 0.5;
            manufacturerButton.titleLabel.font = Font(15);
            [manufacturerButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:manufacturerButton];
        }
    }];
}

//生产商的选择按钮
- (void)click:(UIButton*)btn {
    if (btn.selected == NO) {
        if (_tagArray.count == 5) {
            ALERTSHOW(@"最多可以选择5个生产商");
            return;
        }
        btn.layer.borderColor=[UIColor orangeColor].CGColor;
        NSNumber*number = [NSNumber numberWithInteger:btn.tag];
        [_tagArray addObject:number];
        btn.selected = YES;
    } else {
        NSNumber *number = [NSNumber numberWithInteger:btn.tag];
        [_tagArray removeObject:number];
        btn.layer.borderColor = [UIColor blackColor].CGColor;
        btn.selected = NO;
    }
}

//点击确定按钮执行的方法
- (void)confirmBtnAction {
    NSMutableArray *array=[[NSMutableArray alloc]init];
    for (NSInteger i=0; i<_tagArray.count; i++)
    {
        [array addObject:_dataArray[[_tagArray[i] integerValue]-100][@"CONDITION_NAME"]];
    }
    _stringRow=[array componentsJoinedByString:@","];
    
    self.SelectBlock(_stringRow);
    [self removeSelfAction];
}
-(void)bBtn {
    _stringRow=@"不限";
    self.SelectBlock(_stringRow);
    [self removeSelfAction];
}
//展示出选择器
- (void)showSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0, -260);
    } completion:^(BOOL finished) {
        
    }];
}

//移除选择器
- (void)removeSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform  = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_bgView.frame, point)) {
        return NO;
    }
    
    return YES;
}


@end
